package day14;

import org.junit.Test;

import java.util.*;

public class Find {

    @Test
    public void Date(){
        Random rd=new Random();
        String strs[]={"张三","李四","王五"};
        int z=0;
        int l=0;
        int w=0;
        int end;
        List<People> list=new ArrayList<>();
        for (int i=0;i<100;i++){
            int money=rd.nextInt(10000);
            int n=rd.nextInt(3);
            list.add(new People(strs[n],money));
        }
        Collections.sort(list,((o1, o2) -> o2.recording-o1.recording));
        for (People s:list){
            if (s.name.equals("张三")) z++;
            else if (s.name.equals("李四")) l++;
            else w++;
        }

        for (People s:list){
          if (z>l) {
             end=z;
             if (s.name.equals("张三")){
                 System.out.println("张三消费的次数最多，一共"+end+"次，最高消费为："+s.recording);
                 return;
                 }
             }
        else if (w>z) {
            end=w;
            if (s.name.equals("张三")){
                System.out.println("王五消费的次数最多，一共"+end+"次，最高消费为："+s.recording);
                return;
            }
        }
        else {
            end=l;
            if (s.name.equals("张三")){
                System.out.println("李四消费的次数最多，一共"+end+"次，最高消费为："+s.recording);
                return;
            }
        }
        }
    }
}

class People{
    String name;
    int recording;

    public People(String name, int recording) {
        this.name = name;
        this.recording = recording;
    }
}

class Consumer{
    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        list.add("张三1,1999");
        list.add("张三2,999");
        list.add("张三1,3999");
        list.add("张三3,5999");
        list.add("张三4,6999");
        //统计每个用户消费次数（适合做Map）  key:姓名  value:消费

        Map<String,Integer> map=new HashMap<>();
        for (String s:list){
           String name= s.split(",")[0];
           //非第一次，次数加一
            if (map.containsKey(name)){
                map.put(name,map.get(name)+1);
            }else {//第一次出现，姓名次数赋值为1
                map.put(name,1);
            }
        }
        System.out.println(map);
        //统计消费金额最高的
        int max=0;
        String maxName="";
        for (String k:map.keySet()){
            Integer v=map.get(k);
            if (v>max){
                max=v;
                maxName=k;
            }
        }
        System.out.println("最多消费次数："+maxName);
        double maxMoney=0;
        for (String s:list){
            String[] ss=s.split(",");
            if (ss[0].equals(maxName)&&Integer.parseInt(ss[1])>maxMoney){
                maxMoney=Double.parseDouble(ss[1]);
            }
        }
        System.out.println(maxName+"最高消费金额："+maxMoney);
    }
}